<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ page import="java.util.*" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="java.io.ByteArrayOutputStream" %>
<%@ page import="java.io.ByteArrayInputStream" %>
 <%@ page import="org.json.simple.JSONObject" %>
 <%@ page import="org.json.simple.parser.JSONParser" %>
 <%@ page import="org.json.simple.JSONArray" %>
 <%@ page import="com.netcracker.studportal.server.facade.*" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Admin View - Add Exam</title>
<link href="style/css/addStyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<%
	if ((session.getAttribute("username") == null) || ("").equals(session.getAttribute("username"))) {
		response.sendRedirect("login.jsp");
	}
	
	request.setCharacterEncoding("UTF-8");
	
	String resultMessage = null;
	FacadeAdmin fa = new FacadeAdmin();
	JSONParser parser = new JSONParser();
%>
<div class="header">
    	<div class="wrapper">
    	<a href = "<% out.print("indexAdmin.jsp"); %>" >
        	<div class="logo"> 	
        	<div class="exit"><a href = "<% out.print("logout.jsp"); %>" ><img src="style/img/exit.jpg"></a> </div> 
             <a href = "<% out.print("indexAdmin.jsp"); %>" ><img src="style/img/logo.png" class="log"></a>
            </a>
            </div>
        </div>
    </div>
    <div class="content">
    	<div class="wrapper">
        	<div class="left">
            	<ul class="top-menu">
                    <li><a href="<% out.print("admin.jsp?action=addStudent"); %>">Добавить нового студента</a></li>
                    <li><a href="<% out.print("admin.jsp?action=addProfessor"); %>">Добавить преподавателя</a></li>
                    <li><a href="<% out.print("admin.jsp?action=addExam"); %>">Добавить экзамен</a></li>
                    <li><a href="<% out.print("admin.jsp?action=AssignExam"); %>">Назначить экзамен</a></li>
       
            	</ul>
            </div>
            <div class="right">
            <div class="input_data"><form method="post" action="AddExam.jsp?action=add">
			
			<select name="subject_id" style="width: 155px; ">
			<option value=""></option>
				<%
					String faculty=fa.getAllSubject();
					Object ob = parser.parse(faculty);
					JSONArray array = (JSONArray)ob;
					Iterator iter = array.iterator();
					while(iter.hasNext()) {
						JSONObject data = (JSONObject)iter.next();
						String id = data.get("subject_id").toString();
						String name = data.get("name").toString();
						out.println("<option value=\""+id+"\">"+name+"</option>");
					}
				%>
			</select> Предмет <br>
			
			<select name="prof_id" style="width: 155px; ">
			<option value=""></option>
				<%
					String prof=fa.getAllProfessor();
					Object ob1 = parser.parse(prof);
					JSONArray array1 = (JSONArray)ob1;
					Iterator iter1 = array1.iterator();
					while(iter1.hasNext()){
						JSONObject data = (JSONObject)iter1.next();
						String id = data.get("prof_id").toString();
						String name = data.get("last_name").toString()+" "+data.get("first_name").toString()+" "+data.get("patronymic").toString();
						out.println("<option value=\""+id+"\">"+name+"</option>");
					}		
				%>
			</select> Преподаватель <br>
			
			<input type="text" name="exam_date"> Дата Экзамена <br>
			
			<p class='select'><select name="type" style="width: 155px; ">
			<option value=""></option>
    		<option value="Экзамен">Экзамен</option>
    		<option value="Зачет">Зачет</option>
   			</select> Экзамен/Зачет<br>
			<input type="submit" value="Добавить">
	</form>
<%
	String subject_id = null;
	String exam_date = null;
	String type = null;
	String prof_id = null;

	if ("add".equals(request.getParameter("action"))) {
	if (
		(request.getParameter("subject_id") == null || request.getParameter("subject_id").isEmpty()) ||
		(request.getParameter("exam_date") == null || request.getParameter("exam_date").isEmpty()) ||
		(request.getParameter("type") == null || request.getParameter("type").isEmpty()) ||
		(request.getParameter("prof_id") == null || request.getParameter("prof_id").isEmpty())
	   ) 
	{
		out.println("<font color=red>Необходимо заполнить все поля!</font>");
	} else {
		subject_id = request.getParameter("subject_id");
		exam_date = request.getParameter("exam_date");
		type = request.getParameter("type");
		prof_id = request.getParameter("prof_id");

		JSONObject obj = new JSONObject();
		obj.put("subject_id", subject_id);
		obj.put("exam_date", exam_date);
		obj.put("type", type);
		obj.put("prof_id", prof_id);

		String param = obj.toString();
		String result = fa.addNewExam(param);

		JSONObject jsonObj = (JSONObject) parser.parse(result);
		if ("success".equals(jsonObj.get("status"))) {
			resultMessage = "<font color=green>Экзамен успешно добавлен!</font>";
		} else {
			resultMessage = "<font color=red>При выполнении запроса возникли ошибки.</font>";
		}
	}
}
%>
<%
  if (resultMessage != null) {
	  out.print("Результат: " + resultMessage);
  }
%>
                </div>
            </div>
            <div class="clear"></div>
        </div>
    </div>
    <div class="footer">
    	<div class="wrapper">
        	<div class="copy">
            	<p>Студенческий портал. 2014 &copy; by NetCracker</p>
            </div>
        </div>
    </div>	


</body>
</html>